package com.leyou.gateway.config;

import com.leyou.common.auth.utils.RsaUtils;
import com.leyou.common.enums.LyExceptionEnum;
import com.leyou.common.exception.LyException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PublicKey;

@Slf4j
@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean{
    /**
     * 公钥路径
     */
    private String pubKeyPath;

    /**
     * 公钥对象
     */
    private PublicKey publicKey;

    private UserTokenProperties user = new UserTokenProperties();

    @Data
    public class UserTokenProperties{
        private String cookieName;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【网关】获取公钥失败",e);
            throw new LyException(LyExceptionEnum.INVALID_PARAM_ERROR);
        }
    }
}
